<?php
/**
 * PsiFramework
 *
 * Php Simple Framework
 *
 * PHP version 5.3
 *
 * @category  PsiFramework.Plugins.Form.Classes.Util.Json.DAO
 * @package   PsiFramework.Plugins.Form.Classes.Util.Json.DAO
 * @author    Remi San <remi.san@gmail.com>
 * @copyright 2011 Remi San
 * @license   http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @version   SVN: <svn_id>
 * @link      http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Util\Json\DAO\JsonFormDAO.html
 */

namespace PsiFramework\Plugins\Form\Classes\Util\Json\DAO;

import("Classes.Util.DAO.IPrimitiveObjectDAO");
import("Plugins.Form.Classes.Util.FormUtil");

use \PsiFramework\Plugins\Form\Classes\Util\FormUtil;
use \PsiFramework\Plugins\Form\Classes\Util\Json\JsonForm;
use \PsiFramework\Classes\Model\ModelInterface\IPrimitiveObject;
use \PsiFramework\Classes\Util\DAO\IPrimitiveObjectDAO;
use \PsiFramework\Classes\Util\UI\TemplateManager;

/**
 * The json Form Data Acces Object class
 *
 * This is the json form data acces object
 *
 * @category PsiFramework.Plugins.Form.Classes.Util.Json.DAO
 * @package  PsiFramework.Plugins.Form.Classes.Util.Json.DAO
 * @author   Remi San <remi.san@gmail.com>
 * @license  http://creativecommons.org/licenses/by/3.0/  Creative Commons CC BY 3.0
 * @link     http://www.psiframework.com/phpdoc/classes/db_\PsiFramework\Plugins\Form\Classes\Util\Json\DAO\JsonFormDAO.html
 */
class JsonFormDAO implements IPrimitiveObjectDAO
{
    /**
     * @var JsonFormDAO
     */
    protected static $_singleton = null;

    /**
     * The function used to parse the JsonForm
     * 
     * @param string $value   The string value
     * @param array  $options The options (not used)
     * 
     * @see IPrimitiveObjectDAO::parse()
     * 
     * @return JsonForm
     */
    public function parse($value=null, $options=null)
    {
        $jsonForm = new JsonForm();
        $jsonForm->unserialize($value);
        return $jsonForm ;
    }

    /**
     * The function that returns the html code to edit
     * a json form object
     * 
     * @param IPrimitiveObject $object  The json form object to edit
     * @param string           $name    The name in the html form
     * @param array            $options The options
     * 
     * @see IPrimitiveObjectDAO::edit()
     * 
     * @return string
     */
    public function edit(
        IPrimitiveObject $object=null,
        $name=null,
        $options=null
    )
    {
        $html  = '<div id="fieldTypeCreator">' ;
        $html .= '    <div id="fieldTypeCreatorMenu"></div>' ;
        $html .= '    <div id="fieldTypeCreatorElementsDiv">' ;
        $html .= '        <div id="fieldTypeCreatorStyleClassDiv">' ;
        $html .= '            <span class="label">' ;
        $html .= '                <label for="fieldTypeCreatorClass">Classe</label>' ;
        $html .= '                <input type="text" id="fieldTypeCreatorClass" name="fieldTypeCreatorClass" class="fieldTypeCreatorClass" value="" />' ;
        $html .= '            </span>' ;
        $html .= '            <span class="label">' ;
        $html .= '                <label for="fieldTypeCreatorStyle">Style</label>' ;
        $html .= '                <input type="text" id="fieldTypeCreatorStyle" name="fieldTypeCreatorStyle" class="fieldTypeCreatorStyle" value="" />' ;
        $html .= '            </span>' ;
        $html .= '        </div>' ;
        $html .= '        <ul class="fieldTypeCreatorElements"></ul>' ;
        $html .= '    </div>' ;
        $html .= '</div>' ;
        return $html ;
    }

    /**
     * The function that displays the JsonForm
     * 
     * @param IPrimitiveObject $object  The json form object to display
     * @param array            $options The options
     * 
     * @see IPrimitiveObjectDAO::display()
     * 
     * @return void
     */
    public function display(IPrimitiveObject $object=null, $options=null)
    {

    }

    /**
     * The function that adds the javascript declarations to the template
     *
     * @param IPrimitiveObject $object  The json form object to display
     * @param array            $options The options
     *
     * @see IPrimitiveObjectDAO::javascript()
     *
     * @return void
     */
    public function javascript(IPrimitiveObject $object=null, $options=null)
    {
        $js  = '$(document).ready(function() {' . "\n" ;
        
        $js .= '    var fieldTypeCreatorElements = '.
                        'document.getElementById('.
                            '"fieldTypeCreatorElementsDiv"'.
                        ').'.
                        'getElementsByClassName('.
                            '"fieldTypeCreatorElements"'.
                        ')[0] ;' . "\n" ;

        $js .= '    var fieldTypeCreatorClass = '.
        		'document.getElementById('.
        		'"fieldTypeCreatorStyleClassDiv"'.
        		').'.
        		'getElementsByClassName('.
        		'"fieldTypeCreatorClass"'.
        		')[0] ;' . "\n" ;
        
        $js .= '    var fieldTypeCreatorStyle = '.
                		'document.getElementById('.
                		'"fieldTypeCreatorStyleClassDiv"'.
                		').'.
                        'getElementsByClassName('.
                            '"fieldTypeCreatorStyle"'.
                        ')[0] ;' . "\n" ;
        
        
        if (!is_null($object)) {
            $js .= FormUtil::showForEditFieldType($object);
        }

        $js .= '    $("#fieldTypeCreator").formFieldTypeCreator();' . "\n" ;
        $js .= '});' ;

        TemplateManager::getInstance()->addFileScript(
            'Plugins/Form/Template/scripts/jquery.formFieldTypeCreator.js'
        );
        
        TemplateManager::getInstance()->addFileStyle(
            'Plugins/Form/Template/styles/form.css'
        );
        
        TemplateManager::getInstance()->addLitteralScript($js);
    }

    /**
     * Function to be called instead of the constructor.
     *
     * @return JsonFormDAO
     */
    public static function getInstance()
    {

        if (self::$_singleton==null) {
            self::$_singleton = new JsonFormDAO();
        }

        return self::$_singleton;
    }
}
